<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%!
	public boolean templateIsUsed(String id, Integer hospital) {
		List list = QBuilder.db("basedb").select().from("Visit").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("score=", id)).query().getResultObj();
		if (isEmpty(list))
			return false;
		return true;
	}
%>
<%
	// 模板id
	String templateId = input.getDesc("template_id", "-1");
	Integer uid = token.getUid();
	// 该方案是从官方方案库添加的，根据模板方案id移除
	List<Map> templates = QBuilder.db("basedb").select().from("visit_template").where(new Where("oid =", templateId).and("status = ", STATUS_ENABLE).and("uid=", uid)).query().getResultObj();
	if (isEmpty(templates)) {
		msg = new Message(30041, "没有这个方案");
		// 根据个人方案id移除
		templates = QBuilder.db("basedb").select().from("visit_template").where(new Where("id =", templateId).and("status = ", STATUS_ENABLE).and("uid=", uid)).query().getResultObj();
		if (!isEmpty(templates)) {
			Map template = templates.get(0);
			if (templateIsUsed(templateId, hospital)) {
				msg = new Message(30041, "该方案已被使用，不可被移除");
				Tools.encryptPrint(out, msg);
				return;
			}
			msg = new Message(0, "移除成功");
			template.put("status", STATUS_DISABLE);
			DBTools.db("basedb").modItem("visit_template", template);
		}
	} else {
		Map template = templates.get(0);
		if (templateIsUsed(template.get("id") + "", hospital)) {
			msg = new Message(30041, "该方案已被使用，不可被移除");
			Tools.encryptPrint(out, msg);
			return;
		}
		msg = new Message(0, "移除成功");
		template.put("status", STATUS_DISABLE);
		DBTools.db("basedb").modItem("visit_template", template);
	}
	Tools.encryptPrint(out, msg);
%>